package com.my.hzb.util;

import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SQLCommondUtil<T> {
	
	public Connection conn = null;
	public PreparedStatement pst = null;
	public ResultSet rs = null;
	public int add(String sql){
		int count = 0;
		try {
			pst = conn.prepareStatement(sql);
			count = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	public int update(String sql){
		int count = 0;
		try {
			pst = conn.prepareStatement(sql);
			count = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	public int delete(String sql){
		int count = 0;
		try {
			pst = conn.prepareStatement(sql);
			count = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	public List<T> getAll(String sql){
		List<T> list = new ArrayList<T>();
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				
		    }
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public void setParams(PreparedStatement pst,String parType,int index,Object obj){
		try {
			if("int".equals(parType)||"java.lang.Integer".equals(parType)){
				pst.setInt(index, Integer.parseInt(obj.toString()));
			}else if("long".equals(parType)||"java.lang.Long".equals(parType)){
				pst.setLong(index, Long.parseLong(obj.toString()));
			}else if("double".equals(parType)||"java.lang.Double".equals(parType)){
				pst.setDouble(index, Double.parseDouble(obj.toString()));
			}else if("java.lang.String".equals(parType)){
				pst.setString(index, obj.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
